//
//  WCNAddCustomPartVC.m
//  WCN
//
//  Created by 杨鑫 on 15/6/26.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNAddCustomPartVC.h"
//
#import "NSString+Utilities.h"
//
#import "WCPopupLayerManager.h"
#import "WCNetworkAccessManager.h"
//
#import "Macros.h"

@interface WCNAddCustomPartVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *customPartName;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@end

@implementation WCNAddCustomPartVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"自定义部位";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.saveBtn.layer.cornerRadius = 3;
}

#pragma mark - Actions

- (IBAction)save:(id)sender {
    NSString *name = self.customPartName.text;
    if ([name isEmpty]) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"请输入伤口名称" withView:self.view];
        return;
    }
    
    NSDictionary *params = @{@"parentCode": self.firstPart[@"partCode"],
                             @"partName": name,
                             @"partType": self.firstPart[@"partType"],
                             @"direction": self.firstPart[@"direction"]};
    
    MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
    [[WCNetworkAccessManager sharedMg] POST:@"wound/bodypart/second/add" parameters:params success:^(NSDictionary *result){
        NSDictionary *data = result[@"data"];
        self.wound[@"bodyPartName"] = data[@"partName"];
        self.wound[@"bodyPartCode"] = data[@"partCode"];
        
        NSArray *vcs = self.navigationController.viewControllers;
        UIViewController *vc = vcs[vcs.count-4];
        [self.navigationController popToViewController:vc animated:YES];
    } failure:nil netError:nil always:^{
        [hud hide:YES];
    }];
}

- (IBAction)bgTap:(id)sender {
    [self.view endEditing:YES];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self save:self.saveBtn];
    return YES;
}

@end
